package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AdminGetPrizeRecordRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AdminGetPrizeRecordMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.AdminGetPrizeRecord;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAdminGetPrizeRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author wyh
 * @date 2024-07-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminGetPrizeRecordServiceImpl implements IAdminGetPrizeRecordService {

    private final AdminGetPrizeRecordMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminGetPrizeRecord add(AdminGetPrizeRecordRequest request) {
        AdminGetPrizeRecord entity = AdminGetPrizeRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminGetPrizeRecordRequest request) {
        AdminGetPrizeRecord entity = AdminGetPrizeRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminGetPrizeRecord getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminGetPrizeRecord> pageList(AdminGetPrizeRecordRequest request) {
        Page<AdminGetPrizeRecord> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminGetPrizeRecord> query = this.buildWrapper(request);
        IPage<AdminGetPrizeRecord> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminGetPrizeRecord> list(AdminGetPrizeRecordRequest request) {
        LambdaQueryWrapper<AdminGetPrizeRecord> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminGetPrizeRecord> buildWrapper(AdminGetPrizeRecordRequest param) {
        LambdaQueryWrapper<AdminGetPrizeRecord> query = new LambdaQueryWrapper<>();
        if (param.getInviteActivityId() != null){
               query.eq(AdminGetPrizeRecord::getInviteActivityId ,param.getInviteActivityId());
        }
         if (StringUtils.isNotBlank(param.getUserId())){
               query.like(AdminGetPrizeRecord::getUserId ,param.getUserId());
        }
        if (param.getPrizeId() != null){
               query.eq(AdminGetPrizeRecord::getPrizeId ,param.getPrizeId());
        }
         if (StringUtils.isNotBlank(param.getReplayId())){
               query.like(AdminGetPrizeRecord::getReplayId ,param.getReplayId());
        }
         if (StringUtils.isNotBlank(param.getPrizeGetTime())){
               query.like(AdminGetPrizeRecord::getPrizeGetTime ,param.getPrizeGetTime());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(AdminGetPrizeRecord::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(AdminGetPrizeRecord::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
